package com.google.code.fluentjoiner.io.local;

import com.google.code.fluentjoiner.io.FileObject;
import com.google.code.fluentjoiner.SupplierBuilderException;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LocalFileObject implements FileObject
{
    private static final Logger LOGGER = LoggerFactory.getLogger(LocalFileObject.class);
    private final File file;

    public LocalFileObject(final File file)
    {
        this.file = file;
    }

    @Override
    public byte[] readContent() throws UnsupportedOperationException
    {
        LOGGER.debug("Calling readContent for {}.", file.getAbsolutePath());
        try
        {
            return FileUtils.readFileToByteArray(file);
        }
        catch (final IOException ioException)
        {
            throw new SupplierBuilderException("IOException thrown in readContent.", ioException);
        }
        finally
        {
            LOGGER.debug("Called readContent for {}.", file.getAbsolutePath());
        }
    }

    @Override
    public void writeContent(final byte[] bytes) throws UnsupportedOperationException
    {
        try
        {
            FileUtils.writeByteArrayToFile(file, bytes);
        }
        catch (final IOException ioException)
        {
            throw new SupplierBuilderException("IOException thrown in writeContent.", ioException);
        }
    }
}
